// miniprogram/pages/user/user.js
const util = require('../../utils/util');
const api = require('../../config/api');
Page({
  data: {
    userInfo:{},
    hasUserInfo: 0,
  },
  onLoad(options){

    // 判断session是否有效
    wx.checkSession({
      success: (res) => {
        console.log('session有效');
        
      },
      fail: (res) =>{
        console.log('session失效');
        
      }
    })
    wx.getSetting({
      success(res){
        console.log(res.authSetting)
        if (res.authSetting['scope.userInfo']) {

          console.log("用户授权了");
          
        } else {

          //用户没有授权

          console.log("用户没有授权");

        }
          
      }
    })
  },
  onShow(){
    const userInfo = wx.getStorageSync("userInfo");
    if(userInfo == ''){
        this.setData({
            hasUserInfo: 0,
        });
    }
    else{
        this.setData({
            hasUserInfo: 1,
        });
    }
    this.setData({userInfo})
  },
  //跳转到登录页面
  toLogin(){
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  //跳转到推广页面
  toPromote(){
    // 判断是否已登录
    util.isLogin()
    wx.navigateTo({
        url: '/pages/promote/promote'
    })
  },
  // 清空缓存
  clearStorage(){
    wx.clearStorageSync();
    wx.showLoading({
      title: '加载中...',
    })
    setTimeout(function () {
      wx.hideLoading()
      wx.showToast({
        title: '退出成功',
        success(){
          wx.switchTab({
            url: '/pages/index/index',
          })
        }
      })
     
    }, 1000)
  },
  // 上传文件
  toUpload(){
    wx.navigateTo({
      url: '/pages/upload/upload'
    })
  },
  // 跳到支付页面
  toPay(){
    wx.navigateTo({
      url: '/pages/pay/pay'
    })
  },
  // 获取用户id
  getUserId(){
    wx.request({
      url: api.GetUserId,
      header:{
        token: wx.getStorageSync('token')
      },
      success(res){
        console.log(res)
        let userId = res.data.data;
        console.log(userId)
        wx.showToast({
          title: userId+'',
          icon: 'none',
          duration: 2000
        })
      }
    })
  }
})